<script setup lang="ts">
import { ref, h, shallowRef } from "vue"
import { getElementLabelLine } from "element-tree-line"
import { useMenuStoreHook } from "@/store/modules/menu"
import "element-tree-line/dist/style.css"
import { type Menu } from "@/api/menu/types/menu"

const ElementTreeLine = getElementLabelLine(h)
const useMenuStore = useMenuStoreHook()

// 树型数据
const treeData = ref<Menu[]>()

const copyDfs = (trees: Menu[]) => {
  const result: Menu[] = []
  if (trees) {
    for (let i = 0; i < trees.length; i++) {
      const item = trees[i]
      if (!item.del) {
        result.push(item)
        if (item.child) {
          item.child = copyDfs(item.child)
        }
      }
    }
  }
  return result
}

if (useMenuStore.menus !== null) {
  const menus = [...shallowRef(useMenuStore.menus).value]
  treeData.value = copyDfs(menus)
}

// 树节点属性映射关系
const defaultProps = {
  children: "child",
  label: "meta.title"
}

/**
 * 树组件点击事件句柄方法
 */
const handleNodeClick = (node: any, data: any) => {
  useMenuStore.currendMenuOp(node.data)
  console.log(
    "%c 树组件点击事件句柄方法 %c handleNodeClick",
    "padding: 1px; background-color: #35495e; color: #fff",
    "padding: 1px; background-color: #5e7ce0; color: #fff"
  )
  console.log(
    "%c ∟ %c node %c =>",
    "text-indent: 10px",
    "padding: 1px; background-color: #41b883; color: #fff",
    "color: #000",
    node
  )
  console.log(
    "%c ∟ %c data %c =>",
    "text-indent: 10px",
    "padding: 1px; background-color: #41b883; color: #fff",
    "color: #000",
    data
  )
}
</script>

<template>
  <div class="element-plus-tree">
    <div class="mtc-head">
      <el-icon size="20" color="#606266" class="mtc-head-icon">
        <Menu />
      </el-icon>
      菜单列表
      <el-tooltip effect="dark" placement="right" content="母鸡母鸡">
        <el-icon size="16" color="var(--el-color-primary)" class="mtc-tooltip">
          <QuestionFilled />
        </el-icon>
      </el-tooltip>
    </div>

    <el-tree
      :data="treeData"
      :props="defaultProps"
      :show-checkbox="false"
      :default-expand-all="true"
      :highlight-current="true"
      :expand-on-click-node="false"
    >
      <template #default="{ node, data }">
        <element-tree-line :node="node" :showLabelLine="false" :indent="20">
          <span v-if="!node.isLeaf" style="display: flex; align-items: center">
            <el-icon v-if="node.data.menuType === 'catalogue'" style="margin: 0 6px 0 0px" size="16">
              <SvgIcon v-if="node.meta?.svgIcon" :name="node.meta?.svgIcon" />
              <FolderOpened v-else />
            </el-icon>
            <el-icon v-else-if="node.data.menuType === 'menu'" style="margin: 0 6px 0 0px" size="16">
              <SvgIcon v-if="node.data.meta?.svgIcon" :name="node.data.meta?.svgIcon" />
              <Folder v-else />
            </el-icon>
            <el-icon v-else-if="node.data.menuType === 'permission'" style="margin: 0 6px 0 0px" size="23">
              <SvgIcon name="connection" />
            </el-icon>
            <small @click="handleNodeClick(node, data)">{{ node.data.meta.title }}</small>
          </span>
          <span v-else style="display: flex; align-items: center">
            <el-icon v-if="node.data.menuType === 'permission'" style="margin: 0 6px 0 0px" size="16">
              <SvgIcon name="connection" />
            </el-icon>
            <el-icon v-else style="margin: 0 6px 0 0px" size="16">
              <SvgIcon v-if="node.data.meta?.svgIcon" :name="node.data.meta?.svgIcon" />
              <Document v-else />
            </el-icon>
            <small @click="handleNodeClick(node, data)">{{ node.data.meta.title }}</small>
          </span>
        </element-tree-line>
      </template>
    </el-tree>
  </div>
</template>

<style lang="scss" scoped>
@import url("../../../../assets/font/index.css");

.element-plus-tree {
  box-sizing: border-box;
  // margin-top: 10px;

  .mtc-head {
    display: flex;
    align-items: center;
    font-size: 17px;
    margin-left: -8px;
    color: #606266;
    font-weight: 600;

    .mtc-head-icon {
      margin-right: 8px;
      position: relative;
      top: -1px;
    }

    .mtc-tooltip {
      margin-left: 5px;
      position: relative;
      top: -1px;
    }
  }

  :deep(.el-tree) {
    padding: 10px 0 0 0;
    .el-tree-node__content {
      display: flex;
      font-size: 18px;
      height: 40px;
      i.el-icon {
        margin: 0 5px;
      }
    }
    .el-tree-node {
      // font-family: initial;
      font-size: 16px;
      /** 所有节点 */
      .el-tree-node__expand-icon {
        transform: rotate(0deg) skew(14deg, 0);
        -webkit-transform: rotate(0deg) skew(14deg, 0);
        font-size: 13px;
        svg {
          display: none;
        }
        &::before {
          position: relative;
          left: 2px;
          top: 6px;
          font-family: PixelIcon;
          display: block;
          width: 25px;
          height: 25px;
          content: "\e65b";
        }
      }
      .el-tree-node__expand-icon.expanded {
        transform: skew(14deg, 0);
        -webkit-transform: skew(14deg, 0);
        svg {
          display: none;
        }

        &::before {
          font-family: PixelIcon;
          display: block;
          width: 25px;
          height: 25px;
          content: "\e65a";
        }
      }
      .el-tree-node__expand-icon.is-leaf {
        margin-left: 0;
      }
      .el-checkbox {
        margin-left: 5px;
      }
    }

    /** 所有展开的节点 */
    .el-tree-node.is-current {
      .el-tree-node__content .element-tree-node-label-wrapper {
        .el-icon,
        small {
          color: var(--el-menu-active-color);
        }
      }
      .el-tree-node__children .element-tree-node-label-wrapper {
        .el-icon,
        small {
          color: #606266;
        }
      }
    }
  }
}
</style>
